package com.baizhi.controller;

import com.baizhi.entity.Result;
import com.baizhi.entity.Vipcard;
import com.baizhi.service.VipCardService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@RequestMapping("vipcard")
@Controller
public class VipCardController {

    @Autowired
    private VipCardService vipCardService;

    @RequestMapping("findAll")
    @ResponseBody
    public List<Vipcard> findAll(){
        List<Vipcard> all = vipCardService.findAll();
        return all;
    }

    @RequestMapping("save")
    @ResponseBody
    public Result save(MultipartFile vipcard, Vipcard vipCard,HttpServletRequest request){
        Result result = null;
        String realPath = request.getSession().getServletContext().getRealPath("/back/image/vipcard");
        try{
            String uuid = UUID.randomUUID().toString();
            vipCard.setId(uuid);
            String extension = FilenameUtils.getExtension(vipcard.getOriginalFilename());
            String urlname = uuid+"."+extension;
            String path = "/back/image/vipcard/"+urlname;
            vipCard.setPath(path);
            vipcard.transferTo(new File(realPath,urlname));
            vipCardService.save(vipCard);
            result = new Result();
            result.setStatus(true);
            result.setMessage("添加成功");

        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("添加失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(Vipcard vipcard){
        Result result = null;
        try{
            vipCardService.update(vipcard);
            result = new Result();
            result.setStatus(true);
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("添加失败");
        }
        return result;
    }


    @RequestMapping("findById")
    @ResponseBody
    public Vipcard findById(String id){
        return vipCardService.findById(id);
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(String id,HttpServletRequest request){
        Result result = null;
        try{
            Vipcard vipcard = vipCardService.findById(id);
            String realPath = request.getSession().getServletContext().getRealPath(vipcard.getPath());
            File file = new File(realPath);
            file.delete();
            vipCardService.delete(id);
            result = new Result();
            result.setStatus(true);
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("删除失败");
        }
        return result;
    }
}
